using System;
using UnityEngine;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public enum ComanndType
{
	none,
	ld,
	rd,
	md,
	bg,
	say,
	wait,
	lsp,
	txtbox,
	SoundPlay,
	MusicPlay,
	MusicStop,
}

public class ScriptInterpreter
{
	public class ScriptComannd
	{
		public ComanndType comanndType{get;set;}
		public int Num;
		public float Timer;
		public Vector2 Position;
		public string Name;
	}
	
	public static Queue<ScriptComannd> Interpreter(TextAsset script)
	{
		Regex re = new Regex(@"\r\n");
		string[] Command= re.Split(script.text);
		Queue<ScriptComannd> ComanndQueue = new Queue<ScriptComannd>();
		
		foreach(string s in Command)
		{
			if(s!= null)
			{
				var returenComannd = SwitchComannd(s);
				if(returenComannd != null)
					ComanndQueue.Enqueue(returenComannd);
			}
		}
		
		return ComanndQueue;
	}
	
	static ScriptComannd SwitchComannd(string Command)
	{
		if(Command != "")
		{
			string[] command = Command.Split(' ');
			string[] dir;
			ScriptComannd sc;
			switch(command[0])
			{
				//背景.
				case "bg":
				sc = new ScriptComannd(){comanndType = ComanndType.bg};
				dir = command[1].Split(',');
				if(dir.Length == 1)
				{
					sc.Name = dir[0];
				}
				else if(dir.Length == 2)
				{
					sc.Name = dir[0];
					sc.Timer = float.Parse(dir[1]);
				}
				return sc;
				
				//左立绘.
				case "ld":
				sc = new ScriptComannd(){comanndType = ComanndType.ld};
				dir = command[1].Split(',');
				if(dir.Length == 1)
				{
					sc.Name = dir[0];
				}
				else if(dir.Length == 3)
				{
					sc.Position = new Vector2(float.Parse(dir[0]),float.Parse(dir[1]));
					sc.Name = dir[2];
				}
				return sc;
				
				//右立绘.
				case "rd":
				sc = new ScriptComannd(){comanndType = ComanndType.rd};
				dir = command[1].Split(',');
				if(dir.Length == 1)
				{
					sc.Name = dir[0];
				}
				else if(dir.Length == 3)
				{
					sc.Position = new Vector2(float.Parse(dir[0]),float.Parse(dir[1]));
					sc.Name = dir[2];
				}
				return sc;
				
				case "md":
				sc = new ScriptComannd(){comanndType = ComanndType.md};
				dir = command[1].Split(',');
				if(dir.Length == 1)
				{
					sc.Name = dir[0];
				}
				else if(dir.Length == 3)
				{
					sc.Position = new Vector2(float.Parse(dir[0]),float.Parse(dir[1]));
					sc.Name = dir[2];
				}
				return sc;
				
				//对话.
				case "say":
				sc = new ScriptComannd(){comanndType = ComanndType.say};
				sc.Num = int.Parse(command[1]);
				return sc;
				
				//暂停.
				case "wait":
				sc = new ScriptComannd(){comanndType = ComanndType.wait};
				sc.Timer = int.Parse(command[1]);
				return sc;
				
				case "lsp":
				sc = new ScriptComannd(){comanndType = ComanndType.lsp};
				dir = command[1].Split(',');
				if(dir.Length == 5)
				{
					sc.Num = int.Parse(dir[0]);
					sc.Name = dir[1];
					sc.Position.x = float.Parse(dir[2]);
					sc.Position.y = float.Parse(dir[3]);
					sc.Timer = float.Parse(dir[4]);
				}
				else if(dir.Length == 2)
				{
					sc.Num = int.Parse(dir[0]);
					sc.Name = dir[1];
				}
				return sc;
				
				//textbox
				case "textbox":
				sc = new ScriptComannd(){comanndType = ComanndType.txtbox};
				sc.Num = int.Parse(command[1]);
				return sc;
				
				case "SoundPlay":
				sc = new ScriptComannd(){comanndType = ComanndType.SoundPlay};
				sc.Name = command[1];
				return sc;
				
				case "MusicPlay":
				sc = new ScriptComannd(){comanndType = ComanndType.MusicPlay};
				sc.Name = command[1];
				return sc;
				
				case "MusicStop":
				sc = new ScriptComannd(){comanndType = ComanndType.MusicStop};
				sc.Name = command[1];
				return sc;
			}
		}
		return null;
	}
}

